#!/usr/bin/env python
#
# GAE-Appengine-CMS
# Copyright (c) 2010 ITBelt (itbelt.com)
# Licensed under the GPL license:
# http://www.gnu.org/licenses/gpl.html
#

import string
import sys
from google.appengine.ext import db
from django.template import TemplateDoesNotExist
import os
import models


def import_control(class_name):
    mod = __import__('controls', globals(), locals(), [class_name])
    return getattr(mod, class_name)


def load_template_source(template_name, template_dirs=None):
    tmpl=models.PageTemplate.getByName(template_name)
    if tmpl is None:
        raise TemplateDoesNotExist
    
    if tmpl.controls:
        for c in tmpl.controls.split(";"):
            if c: 
                control=import_control(c)
                control().process()

    s=tmpl.body.encode("UTF-8")
    return (s, None)
load_template_source.is_usable = True  